package jmlzz.example.com.imdemo.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import jmlzz.example.com.imdemo.R;
import jmlzz.example.com.imdemo.bean.Person;
import jmlzz.example.com.imdemo.common.ThreadMannager;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {


    private EditText mEtUsername;
    private EditText mEtPassword;
    private Button mLogin;
    private Button mRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    public String getString(EditText et) {
        if (!TextUtils.isEmpty(et.getText())) {
            return et.getText().toString().trim();
        }
        return null;
    }

    private void initView() {
        mEtUsername = (EditText) findViewById(R.id.et_username);
        mEtPassword = (EditText) findViewById(R.id.et_password);
        mLogin = (Button) findViewById(R.id.login);
        mRegister = (Button) findViewById(R.id.register);
        mLogin.setOnClickListener(this);
        mRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final String username = getString(mEtUsername);
        final String password = getString(mEtPassword);
        switch (v.getId()) {
            case R.id.login:
                if (username != null &&password != null) {
                    EMClient.getInstance().login(username, password, new EMCallBack() {
                        @Override
                        public void onSuccess() {
                            EMClient.getInstance().groupManager().loadAllGroups();
                            EMClient.getInstance().chatManager().loadAllConversations();
                            ToastUtils.showShort("登陆成功");
                            LogUtils.dTag("im","登陆服务器成功");
                            start2Activity(ChatActivity.class);
                        }

                        @Override
                        public void onError(int code, String error) {

                        }

                        @Override
                        public void onProgress(int progress, String status) {
                            LogUtils.dTag("im","登陆服务器失败");
                        }
                    });

                }
                break;
            case R.id.register:


                if (username != null && password != null) {
                    final Person person = new Person();
                    person.setPasswrod(password);
                    person.setUsername(username);
                    person.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                ToastUtils.showShort("bmob 注册成功");
                                ThreadMannager.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            EMClient.getInstance().createAccount(username, password);
                                            ThreadMannager.postMainThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    ToastUtils.showShort("环信 注册成功");
                                                }
                                            });
                                        } catch (HyphenateException e1) {
                                            e1.printStackTrace();
                                            ThreadMannager.postMainThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    person.delete(new UpdateListener() {
                                                        @Override
                                                        public void done(BmobException e) {
                                                            if (e == null) {
                                                                ToastUtils.showShort("bmob 删除用户成功");
                                                            } else {
                                                                ToastUtils.showShort("bmob 删除用户失败");
                                                            }
                                                        }
                                                    });
                                                    ToastUtils.showShort("环信 注册失败");
                                                }
                                            });
                                        }
                                    }
                                });

                            } else {
                                ToastUtils.showShort("bmob 注册失败.." + e.getMessage());
                            }
                        }
                    });
                }
                break;
        }
    }

    private void start2Activity(Class<ChatActivity> chatActivityClass) {
        Intent intent = new Intent(this, chatActivityClass);
        startActivity(intent);
    }
}
